<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../hy/common.jsp"%>
<%@ page import="com.mes.entity.tdd.Staff" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>作业信息扫码页面</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<%--
		<style type="text/css">
			.table-b table td{
				border: 1px solid gray;
			}
		</style>
	--%>
	<script type="text/javascript">
        setInterval("date.innerHTML = new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
	</script>
	<%--<% Staff staff = (Staff)session.getAttribute("staff");%>--%>
	<script type="text/javascript">
        $(function() {
            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();
            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

            var onInput = document.getElementById("trolleyCode");
            onInput.focus();
        });
        var TableInit = function() {
            var url = '${pageContext.request.contextPath }/trolley/queryTrolleyInfoList.do';
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function() {
                $('#waitBindTrolleyList').bootstrapTable({
                    url : url, 				//请求后台的URL（*）
                    method : 'get', 		//请求方式（*）
                    toolbar : '#toolbar', 	//工具按钮用哪个容器
                    striped : true, 		//是否显示行间隔色
                    cache : true, 			//是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination : true, 		//是否显示分页（*）
                    sortable : false, 		//是否启用排序
                    sortOrder : "asc", 		//排序方式
                    exportDataType : "selected",
                    queryParams : oTableInit.queryParams,//传递参数（*）
                    sidePagination : "server", 	//分页方式：client客户端分页，server服务端分页（*）
                    pageNumber : 1, 			//初始化加载第一页，默认第一页
                    pageSize : 10, 				//每页的记录行数（*）
                    pageList : [ 10,20,30 ], 	//可供选择的每页的行数（*）
                    minimumCountColumns : 2, 	//最少允许的列数
                    clickToSelect : true, 		//是否启用点击选中行
                    //height : 523,				//行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId : "ID", 			//每一行的唯一标识，一般为主键列
                    cardView : false, 			//是否显示详细视图
                    detailView : false, 		//是否显示父子表
                    columns : [ {
                        checkbox : true
                    }, {
                        field : 'trolleyCode',
						align : 'center',
                        title : '台车编码'
                    }, {
                        field : 'trolleyName',
                        align : 'center',
                        title : '台车名称'
                    }, {
                        field : 'trolleyStatus',
                        align : 'center',
                        title : '台车状态',
                        formatter:function(value,row,index){ 
			            	var value="";
			            	if(row.trolleyStatus=="1"){
			            		value = "占用";
			            	}else if(row.trolleyStatus=="0"){
			            		value = "空闲";
			            	}
								return value;
			            }
                    }]
                });
                /*$('#waitBindTrolleyList').bootstrapTable('hideColumn', 'id');*/
            };
            //得到查询的参数
            oTableInit.queryParams = function(params) {
                var temp = {
                    limit : params.limit, 					//页面大小
                    page : params.offset/params.limit + 1, 	//页码
                    trolleyName : $("#trolleyName").val()
                };
                return temp;
            };
            return oTableInit;
        };
        var ButtonInit = function() {
            var oInit = new Object();
            oInit.Init = function() {
                //初始化页面上面的按钮事件
            };
            return oInit;
        };

        //注销
		function logOut() {
            window.location.href = "${pageContext.request.contextPath }/toLogin.do";
        }
	</script>
</head>
<body>
	<table class="table-b" style="width: 99.6%; text-align: center; margin: 0px; padding: 0px;" border="1px solid black">
		<tr style="height:90px" ;>
			<td width="21%" style="line-height: 100px;background-color: #337ab7;"><img alt="" src="${pageContext.request.contextPath}/images/logo3.png" width="400px;"></td>
			<td width="21%" style="background-color: #337ab7; font-size: 25px; color:#ffffff; font-weight: bold">台车绑定</td>
			<td width="29%" style="background-color: #337ab7; font-size: 25px; color:#ffffff; font-weight: bold">操作人:张三
				<%--<p style="margin-top: 10px">张三</p>--%>
			</td>
			<td width="29%" style="background-color: #337ab7;">
				<span id="date" style="font-size: 25px; color:#ffffff; font-weight: bold; padding-top: 3px"></span>
			</td>
		</tr>
		<tr style="height:80px">
			<td width="50%" colspan="2" style="background-color: #BDD7EE; font-size: 25px; font-weight: bold">待绑台车</td>
			<td width="16%" style="background-color: #BDD7EE; font-size: 25px; font-weight: bold">台车扫码</td>
			<td width="33%" style="background-color: #BDD7EE; font-size: 25px; font-weight: bold">产品扫码</td>
		</tr>
		<tr>
			<td width="50%" colspan="2" style="text-align: center" width="16%">
				<table id="waitBindTrolleyList" width="99%"></table>
			</td>
			<td width="16%">
				<input class="layui-input" id="trolleyCode" onkeydown="keyDown(event)" type="text" style="width: 99%; height: 72px; text-align: center; font-size: 18px; font-weight: bold" placeholder="台车扫码框">
				<input class="layui-btn" type="button" value="绑定完成" onclick="bindFinish()" style="width: 99%; height: 72px; text-align: center; margin-top: 10px; margin-left: -2px; background: #5FB878">
				<!-- <input class="layui-btn" type="button" value="不良品登记" id="btn" style="width: 99%; height: 72px; text-align: center; margin-top: 10px; margin-left: -2px; background: #337ab7"> -->
				<input class="layui-btn" type="button" value="注销" onclick="logOut()" style="width: 99%; height: 72px; text-align: center; margin-top: 10px; margin-left: -2px; background: #953b39">
			</td>
			<td width="33%">
				<table width="100%" id="myTable">
					<tbody id="tbody">
						<tr id="tr1">
							<td id="td1">
								<input class="layui-input" id="product-code-0" onkeydown="keyDown1(event)" type="text" style="width: 85%; margin: 2px 0px 2px 10px; display: inline-block; border-color: green; font-size: 18px; font-weight: bold">
							</td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
	</table>
	
	
	<!-- 不良品登记-->
	<div class="modal fade " id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModal2">
		<div class="modal-dialog" role="document">
			<div class="modal-content" style="width: 150%;margin-left: -25%;">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close" aria-hidden="true" onclick="resets()">&times;</button>
					<h4 class="modal-title" id="addModalLabel">不良品登记</h4>
				</div>
				<form action="" id="ngForm">
					<table id="mytable" class="table" style="font-size: 24px;">
						<tbody align="center">
							<tr>
								<td width="30%">产品条码：</td>
								<td><input type="text" id="remartk2" name="remartk2"  style="width: 65%;" onblur="checkNogood()"></td>
							</tr>
							<tr>
								<td>不良数量：</td>
								<td>
									<input type="text" id="remartk3" name="remartk3" style="width: 65%;">
								</td>
							</tr>
							<tr>
								<td>不良类型：</td>
								<td>	
									<input type="radio" name="defectivetype1" value="未成形"/> <font>未成形</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="毛边"/> <font>毛边</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="冷料"/> <font>冷料</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="亮印"/> <font>亮印</font>
								</td>
							</tr>
							<tr>
								<td>不良工程：</td>
								<td>
									<input type="radio" name="badproject"  value="注塑"/> <font>注塑</font> &nbsp;&nbsp;
									<input type="radio" name="badproject"  value="组装"/> <font>组装</font>
								</td>
							</tr>
							<tr>
								<td>作业班组：</td>
								<td>
									<input type="radio" name="team" id="day" value="1"> <font>白班</font> &nbsp;&nbsp;
									<input type="radio" name="team" id="night" value="2"> <font>晚班</font>
								</td>
							</tr>
						</tbody>
					</table>
				</form>
				
				<div class="modal-footer">
					<!-- <span id="returnMessage" class="glyphicon"> </span> -->
					<button id="submitBtn1" onclick="ngSave()" type="button" class="btn btn-primary" style="font-size: 26px" aria-hidden="true">保存</button>
					<button type="button" class="btn btn-default right" data-dismiss="modal"style="font-size: 26px">取消</button>
				</div> 
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
    function keyDown(e){
        //IE内核浏览器
        if (navigator.appName == 'Microsoft Internet Explorer'){
            var keycode = event.keyCode;
            //var realkey = String.fromCharCode(event.keyCode);
        }else {
            //非IE内核浏览器
            var keycode = e.which;
            //var realkey = String.fromCharCode(e.which);
        }
        //触发Enter键
        if(keycode == 13){
            $.ajax({
                url : '${pageContext.request.contextPath }/trolley/queryTrolleyByTrolleyCode.do',
				type : 'post',
				data : {trolleyCode: $("#trolleyCode").val()},
				async:false,
				success : function(data) {
                    var data = eval("("+data+")");
					if(data[0].trolleyStatus == '0'){
                        toastr.success("该台车处于空闲状态，可以绑定！");
                        $("#trolleyCode").attr("readonly", "readonly");
						var input0 = $("#product-code-0");
						input0.focus();
					} else if (data[0].trolleyStatus == null) {
                        toastr.warning("可能不是台车条码，请重新扫码！");
                        $("#trolleyCode").val("");
					} else {
                        toastr.warning("该台车处于正在使用状态，不可以绑定！");
                        $("#trolleyCode").val("");
					}
				}
        	});
        }
    }

	//产品扫码框的扫码事件
    var count = 0;
    function keyDown1(e){
        if (navigator.appName == 'Microsoft Internet Explorer'){
            var keycode = event.keyCode;
        }else {
            var keycode = e.which;
        }
        if(keycode == 13) {
            count ++;
            /!*————————————————————————————————————————————————————————————*!/
            var index;
            var index1;
            var rowId = $("#product-code-0").parent().parent().attr('id');
            var tbody = document.getElementById("tbody");
            index = tbody.rows.length;
            index1 = tbody.rows.length+1;
            var tab = document.getElementById('myTable');
            //指定行号
            var rowNumNext = document.getElementById(rowId).rowIndex+1;
            //在指定行下插入一行
            var tr = tab.insertRow(rowNumNext);
            //给tr设置动态Id和样式
            tr.id = "tr" + index1;
            tr.style = "text-align: center; height: 30px;";
            //在行下插入列内容
            var cell0 = tr.insertCell(0);
            cell0.innerHTML = '<td><label style="font-size: 20px; font-weight: bold; color: #337ab7">'+index+'</label><span id="product-code-'+index+'" style="width: 85%; margin-left: 10px; font-weight: bold; font-size: 18px"></span></td>';
            /!*————————————————————————————————————————————————————————————*!/
            console.log(count);
			$("#product-code-"+count).html($("#product-code-0").val())
            $("#product-code-0").val("")
        }
    }
    //按键事件
    //document.onkeydown = keyDown1;
    //提示框的位置设置为中上
    toastr.options.positionClass = 'toast-top-center';
    //自动关闭时间
	toastr.options.timeOut = '2000';

	/*绑定完成*/
    function bindFinish(){
        $.ajax({
            type : 'post',
            url : '${pageContext.request.contextPath }/trolley/updateTrolleyInfoStatusByTrolleyCode.do',
            data : {trolleyCode: $("#trolleyCode").val()},
            async: false,
            success : function(res) {
                if (res > 0) {
                    $("#tbody tr").each(function(i,dom){
                        var tr = $(dom).closest("tr");
                        var tds = tr.find("td");
                        //主表插入行ID
                        var tId = res;
                        //var productCode = tds[0].getElementsByTagName("span")[0].value;
                        var productCode = tr.find('td:first').children('span').text();
                        console.log(productCode);
                        $.ajax({
                            type:'post'
                            ,url:'${pageContext.request.contextPath}/trolley/addTrolleyInfo_ProductCode.do'
                            ,data:{tId : tId, productCode : productCode}
                            ,dataType:'json'
                            ,success:function(multRes){
                                if(multRes > 0){
                                    console.log("绑定完成");
								}
                            }
                        })
                    });
                    /*layerTips.msg("添加成功");*/
                    toastr.success("绑定完成！");
                    setTimeout(function(){
                        window.location.reload();
                    },2000);
                    $("#trolleyCode").val("")
                    $('#addModal').modal('hide');
                }
            }
        });
	}
</script>

<script type="text/javascript">
$(function () {
	
	//清除弹窗原数据
	$("#myModal2").on("hidden.bs.modal", function() {
	    $(this).removeData("bs.modal");
	});
	
    //打开 不良登记
    $("#btn").click(function(){
    	$("#ngForm")[0].reset();//打开模态框前进行重置
    	$("#remartk2").focus(); 
		$("#remartk2").select(); 
    	
    	$("#btn").attr("data-toggle","modal");
    	$("#btn").attr("data-target","#myModal2");
    });
    
});

/*不良登记 - 条码校验 */
function checkNogood() {
	var code = $(remartk2).val();
	//校验条码
	$.ajax({
		type:"post",
		async:false, 
		url: '${pageContext.request.contextPath }/code/checkNogoodCode.do',
		data: {code:code},
		success: function(data){
			if(data >0) {
				 toastr.warning("对不起，此产品已做不良登记！");
			}else if(data < 0) {
				 toastr.warning("对不起，产品中找不到该条码！");
			}
		}
	});
}

//保存不良登记
function ngSave() {
	//获取不良登记的班组
	var team = $('input[name="team"]:checked ').val();
	var defectivetype1 = $('input[name="defectivetype1"]:checked ').val();
	var badproject = $('input[name="badproject"]:checked ').val();
	var dev = $("#deviceId").val();
	var number = $("#number").text();
	var remartk2 = $("#remartk2").val();
	var remartk3 = $("#remartk3").val();
	var pdid = $("#pdid").val();
	if(defectivetype1 == null){
		toastr.warning("请先选择不良类型");
	}else if(team == "" || team == null  || team == undefined) {
		toastr.warning("请选择班组");
	}else {
		$.ajax({
			type:"post",
			url: '${pageContext.request.contextPath }/homeworkInfo/addNoGood.do',
			data: {defectivetype1:defectivetype1,badproject:badproject,team:team,remartk:dev,remartk1: number,remartk2: remartk2,remartk3: remartk3,remartk4: pdid},
			success: function(data){
				if(data){
					toastr.success("添加成功");
					selectNogood();
					createWork();
					$("#addForm")[0].reset();//进行重置
					$("#icode").focus(); 
					$("#icode").select(); 
				}else {
					toastr.error("添加失败");
				}
			}
		});
	}
}
</script>

</html>